<?php

namespace Admin\Controller;

use User\Api\UserApi;
use Org\Util\Sync;
use Think\Model;
use Org\Util\Rest;
use Org\BMap; 

/**
 * 代购单控制器
 * @author zhangwei
 * 2015-06-17
 */
class AgentController extends AdminController {

    /**
     * 代购单详情
     * @author scor
     * 2015-06-18
     */
    public function Agentdetails($orderid = null) {
        $map['orderid'] = $orderid;
        $orderlist = D('agent_order')->where($map)->select();
        sg_dg_int_to_string_order($orderlist);
        $order = $orderlist[0];
        $order['order_goods'] =D('oderder')->query("select * from gr_agent_order_goods where orderid = '".$map['orderid']."'");
        $this->order = $order;
        
        $this->display();
    }

    /**
     * 撤销代购单
     * @author scor
     * 2015-06-18
     */
    public function ajaxAgentCancel($orderid){
        $data['orderid']=$orderid;
        $orderStatus['status']=4000;
        if(D('agent_order')->where($data)->save($orderStatus)){
             $this->success("代购单取消成功");
        }
    }
    /**
     * 受理订单
     * @author scor
     * 2015-06-18
     */
    public function ajaxAgentTaken($orderid){
        $data['orderid']=$orderid;
        $orderStatus['status']=2000;
        if(D('agent_order')->where($data)->save($orderStatus)){
             $this->success("代购单受理成功");
        }else{
            $this->error("代购单受理失败");
        }
    }
    /**
     * 代购单成功
     * @author scor
     * 2015-06-18
     */
        public function ajaxAgentSuccess($orderid){
        $data['orderid']=$orderid;
        $orderStatus['status']=3000;
        if(D('agent_order')->where($data)->save($orderStatus)){
             $this->success("代购单成功");
        }else{
            $this->error("代购单失败");
        }
    }

    /**
     *   代购单管理
    *@author by scor
     */
    public function agent(){
        $id = I('id');
        $status = I('status');
        $b_date = I('b_date');
        $e_date = I('e_date');
        if ($id) {
            $map['orderid']=array('like','%'.$id.'%');
        }
        if ($status != null && $status != 0) {
            $map['status']=$status;
        }
        if ($b_date && $e_date) {
            $map['createtime']=array('between',array(strtotime($b_date),strtotime($e_date)));
        }else if($b_date){
            $map['createtime']=array('egt',strtotime($b_date));
        }else if($e_date) {
            $map['createtime']=array('elt',strtotime($e_date));
        }
        $list = $this->listsM('agent_order', $map,'create_time desc');
        sg_dg_int_to_string_order($list);
        $this->assign('_list', $list);
        $this->assign('_status', $status);
        $this->display();
    }

    /**
     * 代购单修改
     * @author Scor 2015-10-11
     */
    public function confirm(){

//        $oldOrder=D('order')->where($map)->find();
//        $data2['ordernum']=$oldOrder['ordernum'];
//        D('order')->where($map)->save($data2);
        $orderPrice=0;
        $orderMap['orderid']=I('orderid');
        $num=I('num');
        $id=I('id');
        $totalprices=I('totalprice');
        $prices=I('price');
        for($i=0;$i<count($prices);$i++){
            $map['id']=$id[$i];
            $data['num']=$num[$i];
            $data['totalprice']=$totalprices[$i];
            $data['price']=$prices[$i];
            $orderPrice+=$data['totalprice'];
            D('agent_order_goods')->where($map)->save($data);
        }
        $orderData['orderprice']=$orderPrice;
        D('agent_order')->where($orderMap)->save($orderData);
        $this->success("修改成功!", U('Agent/agentdetails?orderid='.I('orderid')),1);
    }

}
